package com.webcamtracker.tracker;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


public class TrackHistory<T> implements Iterable<TrackHistory.TrackerHistoryElement<T>> {
    private List<TrackerHistoryElement<T>> stack;
    private int maxSize;

    public TrackHistory(int maxSize) {
        this.maxSize = maxSize;
        stack = new LinkedList<TrackerHistoryElement<T>>();
    }

    public T pop() {
        return isEmpty() ? null : stack.remove(0).getModel();
    }

    public T peek() {
        return isEmpty() ? null : stack.get(0).getModel();
    }

    public TrackerHistoryElement<T> get(int i) {
        return stack.get(i);
    }

    public int size() {
        return stack.size();
    }

    public void push(T element, long now) {
        stack.add(0, new TrackerHistoryElement<T>(now, element));
        while (stack.size() >= maxSize) {
            stack.remove(stack.size() - 1);
        }
    }

    public boolean isEmpty() {
        return stack.isEmpty();
    }

    public void clear() {
        stack.clear();
    }

    public Iterator<TrackerHistoryElement<T>> iterator() {
        return stack.iterator();
    }


    public static class TrackerHistoryElement<T> {
        private long time;
        private T model;

        private TrackerHistoryElement(long time, T model) {
            this.time = time;
            this.model = model;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public T getModel() {
            return model;
        }

        public void setModel(T model) {
            this.model = model;
        }
    }
}
